﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace Exones.Blend.AssetsManager
{
	public class TreeViewHelper
	{
		private static Dictionary<DependencyObject, TreeViewSelectedItemBehavior> behaviors = new Dictionary<DependencyObject, TreeViewSelectedItemBehavior>();

		public static object GetSelectedItem(DependencyObject obj)
		{
			return (object)obj.GetValue(SelectedItemProperty);
		}

		public static void SetSelectedItem(DependencyObject obj, object value)
		{
			obj.SetValue(SelectedItemProperty, value);
		}

		// Using a DependencyProperty as the backing store for SelectedItem.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty SelectedItemProperty =
			DependencyProperty.RegisterAttached("SelectedItem", typeof(object), typeof(TreeViewHelper), new UIPropertyMetadata(null, SelectedItemChanged));

		private static void SelectedItemChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
		{
			if (!(obj is TreeView))
				return;

			if (!behaviors.ContainsKey(obj))
				behaviors.Add(obj, new TreeViewSelectedItemBehavior(obj as TreeView));

			TreeViewSelectedItemBehavior view = behaviors[obj];
			view.ChangeSelectedItem(e.NewValue);
		}

		private class TreeViewSelectedItemBehavior
		{
			TreeView view;
			public TreeViewSelectedItemBehavior(TreeView view)
			{
				this.view = view;
				view.SelectedItemChanged += (sender, e) => SetSelectedItem(view, e.NewValue);
			}

			internal void ChangeSelectedItem(object p)
			{
				TreeViewItem item = (TreeViewItem)view.ItemContainerGenerator.ContainerFromItem(p);
				item.IsSelected = true;
			}
		}
	}
}
